#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/uaccess.h>
#define CNAME "myled"
#define COUNT 3
struct cdev* cdev;
int major = 238;
int minor = 0;
char kbuf[128] = { 0 };
struct class* cls;
struct device* dev;

//定义并初始化原子操作
atomic_t atm = ATOMIC_INIT(1);

int myled_open(struct inode* inode, struct file* filp)
{
    //减去1之后和0比较，如果结果为0，表示获取锁成功，获取锁
    //成功返回真。
    if(!atomic_dec_and_test(&atm)){
        //失败会进入这个if语句，当进来的时候这个数据就从0减到了-1
        //在退出open函数前，需要将它减去的数值在加回来
        atomic_inc(&atm);
        return -EBUSY;
    }
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}

ssize_t myled_read(struct file* filp,
    char __user* ubuf, size_t size, loff_t* offs)
{
    int ret;

    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);

    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
    ret = copy_to_user(ubuf, kbuf, size);
    if (ret) {
        printk("copy data to user error\n");
        return -EINVAL;
    }

    return size;
}

ssize_t myled_write(struct file* filp,
    const char __user* ubuf, size_t size, loff_t* offs)
{
    int ret;
    
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    if (size > sizeof(kbuf))
        size = sizeof(kbuf);
        
    ret = copy_from_user(kbuf, ubuf, size);
    if (ret) {
        printk("copy data from user error\n");
        return -EINVAL;
    }


    return size;
}

int myled_close(struct inode* inode, struct file* filp)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    atomic_inc(&atm);
    return 0;
}

const struct file_operations fops = {
    .open = myled_open,
    .read = myled_read,
    .write = myled_write,
    .release = myled_close,
};

static int __init myled_init(void)
{
    int ret, i;
    dev_t devno;
    // 1.分配对象
    cdev = cdev_alloc();
    if (cdev == NULL) {
        printk("alloc cdev memory error\n");
        ret = -ENOMEM;
        goto ERR1;
    }

    // 2.对象初始化
    cdev_init(cdev, &fops);

    // 3.设备号的申请
    if (major == 0) {
        //动态申请
        ret = alloc_chrdev_region(&devno, minor, COUNT, CNAME);
        if (ret) {
            printk("dynamic:request device number error\n");
            goto ERR2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    } else {
        //静态指定
        ret = register_chrdev_region(MKDEV(major, minor), COUNT, CNAME);
        if (ret) {
            printk("static:request device number error\n");
            goto ERR2;
        }
    }

    // 4.字符设备驱动的注册
    ret = cdev_add(cdev, MKDEV(major, minor), COUNT);
    if (ret) {
        printk("register chardev error\n");
        goto ERR3;
    }

    // 5.自动创建设备节点
    cls = class_create(THIS_MODULE, CNAME);
    if (IS_ERR(cls)) {
        printk("class create error\n");
        ret = PTR_ERR(cls);
        goto ERR4;
    }

    for (i = minor; i < minor + 3; i++) {
        dev = device_create(cls, NULL, MKDEV(major, i), NULL, "myled%d", i);
        if (IS_ERR(dev)) {
            printk("device create error\n");
            ret = PTR_ERR(dev);
            goto ERR5;
        }
    }

    return 0; /*!!!!!!!!!!!!!!不要忘记!!!!!!!!!!!!!!!!!!!!!!!*/
ERR5:
    for (--i; i >= minor; i--) {
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(MKDEV(major, minor), COUNT);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}
static void __exit myled_exit(void)
{
    int i;
    //注销设备节点
    for (i = minor; i < minor + 3; i++) {
        device_destroy(cls, MKDEV(major, i));
    }
    class_destroy(cls);
    //字符设备驱动的注销
    cdev_del(cdev);
    //设备号释放
    unregister_chrdev_region(MKDEV(major, minor), COUNT);
    //释放cdev的内存空间
    kfree(cdev);
}
module_init(myled_init);
module_exit(myled_exit);
MODULE_LICENSE("GPL");